<?php
/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
function displayBuzzPost($buzz, $post) {
  //print_r($post);
  $titleLink = isset($post->links['alternate'][0]->href) ? $post->links['alternate'][0]->href : '#';
  $author = isset($post->person->name) ? $post->person->name : "Anonymous";
  $authorLink = isset($post->person->profileUrl) ? $post->person->profileUrl : "#";
  $date = isset($post->published) ? date("F jS, Y", strtotime($post->published)) : "";
  
  $content = '';
  
  if (isset($post->object)) {
    switch ($post->object->type) {
      case 'note':
        $content = $post->object->content;
        break;
      default:
        // at the moment buzz only produces note objects, so anything else is unsupported
        $content = "<span class='error'>Unsupported activity object type: '{$post->object->type}'</span><br>";
        break;
    }
  } else {
    echo "[displayBuzzPost] object not set!<br>";
    // this happens on posts with a visibility set, currently private posts are not accessable through the REST API
  }

  $photoContent = $articleContent = $videoContent = '';
  if (isset($post->object->attachments)) {
    foreach ($post->object->attachments as $attachment) {
      switch ($attachment->type) {
        case 'article':
          $aTitle = isset($attachment->title) ? $attachment->title : null;
          $aContent = isset($attachment->content) ? $attachment->content : null;
          $aLink = isset($attachment->links['alternate'][0]->href) ? $attachment->links['alternate'][0]->href : null;
          $articleContent .= "<br /><p><a target='_blank' class='articleLink' href='$aLink'>$aTitle</a> $aContent<p>";
          break;
        case 'photo':
          $url = isset($attachment->links['preview'][0]->href) ? $attachment->links['preview'][0]->href : null;
          $enclosure = isset($attachment->links['enclosure'][0]->href) ? $attachment->links['enclosure'][0]->href : null;
          $onClick = $enclosure ? " onClick='window.location='$enclosure''" : '';
          //FIXME need to add proper click cursor here!
          $photoContent .= "<div $onClick class='photo' style='background-image : url(" . htmlentities($url) . ");'></div>";
          break;
        case 'photo-album':
          if (isset($attachment->links['alternate'][0]->href)) {
            $articleContent .= "<br><br><a target='_blank' href='{$attachment->links['alternate'][0]->href}'>{$attachment->title}</a><br>";
          }
          if (isset($attachment->content)) {
            $articleContent .= $attachment->content . "<br>";
          }
          break;
        case 'video':
          $flashUrl = isset($attachment->links['alternate'][0]->href) ? str_replace('&autoplay=1', '', $attachment->links['alternate'][0]->href) : null;
          $videoContent .= "
  			<object width='425' height='344'>
    				<param name='movie' value='$flashUrl'></param>
    				<param name='allowFullScreen' value='true'></param>
    				<param name='allowscriptaccess' value='always'></param>
    				<embed src='$flashUrl' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='425' height='344'></embed>
  			</object>\n";
          break;
        default:
          $content = "<span class='error'>Unsupported attachment type:</span><pre>\n".print_r($attachment, true)."</pre>";
      }
    }
  }
  echo "
  		<div class='buzzPost'>
  		  <div class='buzzTitle'>
  		    <a href='$titleLink' target='_blank'>$date</a>, by <a href='$authorLink' target='_blank'>$author</a>
  		  </div>
  			<div class='buzzContent'>
  			  $content
  				$articleContent";

  if (! empty($photoContent)) {
    echo "<div class='photos'>$photoContent</div>";
  }
  if (! empty($videoContent)) {
    echo "<div class='photos video'>$videoContent</div>";
  }

  if ((isset($post->liked) && count($post->liked)) || (isset($post->comments) && count($post->comments))) {
    echo "<div class='responseHeader'></div>";
  }

  if (isset($post->liked) && count($post->liked)) {
    $implode = array();
    foreach ($post->liked as $person) {
      $implode[] = "<a href='$person->profileUrl' target='_blank'>{$person->name}</a>";
    }
    echo "<div class='likes'>" . count($implode) . " people liked this: " . implode(', ', $implode) . "</div>";
  }

  if (isset($post->comments) && count($post->comments)) {
    foreach ($post->comments as $comment) {
      //FIXME add $comment->person->thumbnailUrl
      echo "<div class='comment'><div class='commentThumb'><img src='{$comment->person->thumbnailUrl}' width='35' height='35' alt='' /></div><a target='_blank' class='person' href='{$comment->person->profileUrl}'>{$comment->person->name}</a> {$comment->content}</div>";
    }
  }

  echo "
	    	</div>
    	</div>\n";
}